package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> list = dishMapper.listWithCategory(dishPageQueryDTO.getCategoryId(), dishPageQueryDTO.getName(),
                dishPageQueryDTO.getStatus());



        return new PageResult(list.getTotal(),list.getResult());
    }

    @Override
    @Transactional
    public DishVO getByIdWithFlavor(Long id) {

        // DishVO dishVO=new DishVO();

        DishVO dishVO=dishMapper.getByIdWithCategory(id);

        List<DishFlavor> dishFlavors= dishFlavorMapper.listByDishId(id);
        dishVO.setFlavors(dishFlavors);



        return dishVO;

    }

    @Override
    @Transactional //这里有多个数据库操作，需要使用事务
    public void insertWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向dish表插入一条数据
        dishMapper.insert(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&& !flavors.isEmpty()){
            //向dish_flavor插入n条数据

            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
               // dishFlavorMapper.insert(flavor);
            }
            //使用批量插入
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    @Transactional //删掉对应的dish_flavor中的数据
    public void deleteBatch(List<Long> ids) {
        dishFlavorMapper.deleteBatch(ids);

        dishMapper.deleteBatch(ids);

    }

    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //向dish更新数据
        dishMapper.update(dish);

        //向dish_flavor更新数据
        //因为flavor有增加，减少，修改等多种可能，更新时采用先删除再插入

        //删除
        ArrayList<Long> ids=new ArrayList<>();
        ids.add(dish.getId());
        dishFlavorMapper.deleteBatch(ids);

        //插入
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null &&!flavors.isEmpty()){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public void modifyStatus(Integer status, Long id) {

        Dish dish=Dish.builder()
                .status(status)
                .id(id).build();

        //dishMapper.modifyStatus(status,id);
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> listWithFlavor(Long categoryId) {

        List<Dish> dishes=dishMapper.listByCategoryId(categoryId);

        List<DishVO> dishVOList=new ArrayList<>();

        dishes.forEach(dish -> {
            DishVO dishVO=new DishVO();
            List<DishFlavor> dishFlavors = dishFlavorMapper.listByDishId(dish.getId());
            BeanUtils.copyProperties(dish,dishVO);
            dishVO.setFlavors(dishFlavors);
            dishVOList.add(dishVO);
        });

        return dishVOList;
    }

    @Override
    public List<DishVO> listWithFlavorEnabled(Long categoryId) {

        List<Dish> dishes=dishMapper.listByCategoryId(categoryId, StatusConstant.ENABLE);
        List<DishVO> dishVOList=new ArrayList<>();
        dishes.forEach(dish -> {
            DishVO dishVO=new DishVO();
            List<DishFlavor> dishFlavors = dishFlavorMapper.listByDishId(dish.getId());
            BeanUtils.copyProperties(dish,dishVO);
            dishVO.setFlavors(dishFlavors);
            dishVOList.add(dishVO);
        });

        return dishVOList;
    }

    @Override
    public List<Dish> listByCategoryId(Long categoryId) {
        return dishMapper.listByCategoryId(categoryId);
    }


}
